import { makeAutoObservable, action } from 'mobx'
import { Record } from '../../types';
import { request } from '@/services/httpRequest';
import Taro from '@tarojs/taro';

// 测试数据
const mockRecords: Record[] = [
  {
    id: '1',
    category: '2',
    title: '早餐麦片',
    content: '燕麦片配牛奶和蓝莓',
    mood: '😐',
    images: ['https://img.zcool.cn/community/01f5ce56e282f232f875520f7b67cb.jpg'],
    date: new Date().toISOString().split('T')[0],
  },
  {
    id: '2',
    category: '2',
    title: '午餐咖喱饭',
    content: '日式咖喱配米饭，加温泉蛋',
    mood: '😐',
    images: ['https://img.zcool.cn/community/01639e56e282f232f875520f23509b.jpg'],
    date: new Date().toISOString().split('T')[0],

  },
  {
    id: '3',
    category: '2',
    title: '晨跑4KM',
    content: '在公园中心绕跑两圈',
    mood: '😐',
    images: ['https://img.zcool.cn/community/01270156e282f232f875520f1c2088.jpg'],
    date: new Date().toISOString().split('T')[0],

  },
  {
    id: '4',
    category: '1',
    title: '瑜伽课',
    content: '初级瑜伽课程，专注于拉伸',
    mood: '😐',
    images: ['https://img.zcool.cn/community/01f9c656e282f232f875520f0d3074.jpg'],
    date: new Date().toISOString().split('T')[0],

  },
  {
    id: '5',
    category: '2',
    title: '晚餐沙拉',
    content: '蔬菜沙拉配烤鸡胸肉',
    mood: '😐',
    images: ['https://img.zcool.cn/community/013c1256e282f232f875520f789e97.jpg'],
    date: new Date().toISOString().split('T')[0],

  },
  {
    id: '6',
    category: '2',
    title: '晚餐沙拉',
    content: '蔬菜沙拉配烤鸡胸肉',
    mood: '😐',
    images: ['https://img.zcool.cn/community/013c1256e282f232f875520f789e97.jpg'],
    date: new Date().toISOString().split('T')[0],

  }
]

export class RecordStore {
  constructor() {
    makeAutoObservable(this);
  }

  records: Record[] = mockRecords;
  selectedDate: string = new Date().toISOString().split('T')[0];



  setSelectedDate(date: string) {
    this.selectedDate = date;
  }

  addRecord = async (record: Record) => {
    // const newRecord = {
    //   ...record,
    //   id: Date.now().toString(),
    // };
    // this.records.push(newRecord);

    try {
      const res = await request({
        url: '/api/record/add',
        method: 'POST',
        data: record
      });
      console.log(res, 'res');

    } catch (error) {
      Taro.showToast({
        title: '添加失败' + error,
        icon: 'none',
      });
    }
  }

  updateRecordStatus(id: string, status: 'planned' | 'completed') {
    const record = this.records.find(r => r.id === id);
    if (record) {
      record.status = status;
    }
  }

  get todayRecords() {
    return this.records.filter(record => record.date === this.selectedDate);
  }
}

export const recordStore = new RecordStore();

// function makeAutoObservable<RecordStore extends RecordStore>(arg0: this) {
//   throw new Error('Function not implemented.');
// }
